﻿using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace TSPquestEDITOR
{
    public partial class basic_info : Form
    {

        delegate void apply_list_delegate();

        public void load_quest_objectives()
        {
            SqlConnection qCon = new SqlConnection(connectionString);
            qCon.Open();

            SqlCommand command = new SqlCommand(
            "SELECT value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11, value12 FROM dbo.QuestResource WHERE id = '" + fin_qID + "'",
            qCon);

            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    quest_obj_1_id_val = reader.GetInt32(0);
                    quest_obj_1_amount_val = reader.GetInt32(1);
                    quest_obj_2_id_val = reader.GetInt32(2);
                    quest_obj_2_amount_val = reader.GetInt32(3);
                    quest_obj_3_id_val = reader.GetInt32(4);
                    quest_obj_3_amount_val = reader.GetInt32(5);
                }
            }
            reader.Close();

            MessageBox.Show(quest_obj_3_id_val.ToString());

            //If quest has an available Objective 1
            if (quest_obj_1_id_val != 0)
            {
                bgw.ReportProgress(32, "READING QUEST OBJECTIVE 1");

                //If the quest type is appropriate lets load the info about it
                //Remember some quests don't have proper info that can be displayed
                //Depending on Quest TYPE we select Objective Name
                if (quest_type_val == "KILL_TOTAL" || quest_type_val == "KILL_INDIVIDUAL")
                {
                    //Load 1st Objective Strings from Monsters
                    SqlCommand quest_type_string = qCon.CreateCommand();
                    quest_type_string.CommandText = "SELECT SRES.value FROM dbo.QuestResource QRES INNER JOIN dbo.MonsterResource MRES ON MRES.id = QRES.value1 INNER JOIN dbo.StringResource SRES ON SRES.code = MRES.name_id WHERE MRES.id = '" + quest_obj_1_id_val + "'  ";
                    string quest_type_string_res = ((string)quest_type_string.ExecuteScalar());
                    quest_obj_1_name_val = quest_type_string_res.ToString();

                    //Check for Objective 2
                    if (quest_obj_2_id_val != 0)
                    {
                        bgw.ReportProgress(34, "READING QUEST OBJECTIVE 2");

                        //Load 2nd Objective Strings From Monsters
                        SqlCommand quest_type_2_string = qCon.CreateCommand();
                        quest_type_2_string.CommandText = "SELECT SRES.value FROM dbo.QuestResource QRES INNER JOIN dbo.MonsterResource MRES ON MRES.id = QRES.value3 INNER JOIN dbo.StringResource SRES ON SRES.code = MRES.name_id WHERE MRES.id = '" + quest_obj_2_id_val + "'  ";
                        string quest_type_2_string_res = ((string)quest_type_2_string.ExecuteScalar());
                        quest_obj_2_name_val = quest_type_2_string_res.ToString();
                    }

                    //Check for Objective 3
                    if (quest_obj_3_id_val != 0)
                    {
                        bgw.ReportProgress(36, "READING QUEST OBJECTIVE 3");

                        //Load 3rd Objective Strings From Monsters
                        SqlCommand quest_type_3_string = qCon.CreateCommand();
                        quest_type_3_string.CommandText = "SELECT SRES.value FROM dbo.QuestResource QRES INNER JOIN dbo.MonsterResource MRES ON MRES.id = QRES.value5 INNER JOIN dbo.StringResource SRES ON SRES.code = MRES.name_id WHERE MRES.id = '" + quest_obj_3_id_val + "'  ";
                        string quest_type_3_string_res = ((string)quest_type_3_string.ExecuteScalar());
                        quest_obj_3_name_val = quest_type_3_string_res.ToString();
                    }

                    //Set Quest Type Internal Reference
                    quest_type_internal_reference = "MONSTER";
                }
                if (quest_type_val == "COLLECT" || quest_type_val == "RANDOM_COLLECT" || quest_type_val == "HUNT_ITEM" || quest_type_val == "UPGRADE_ITEM")
                {
                    //Load Strings from Items
                    SqlCommand quest_type_string = qCon.CreateCommand();
                    quest_type_string.CommandText = "SELECT SRES.value FROM dbo.ItemResource IRES INNER JOIN dbo.StringResource SRES ON SRES.code = IRES.name_id WHERE IRES.id = '" + quest_obj_1_id_val + "'";
                    string quest_type_string_res = ((string)quest_type_string.ExecuteScalar());
                    quest_obj_1_name_val = quest_type_string_res.ToString();

                    //Check for Objective 2
                    if (quest_obj_2_id_val != 0)
                    {
                        bgw.ReportProgress(34, "READING QUEST OBJECTIVE 2");
                        //Load Strings from Items
                        SqlCommand quest_type_2_string = qCon.CreateCommand();
                        quest_type_2_string.CommandText = "SELECT SRES.value FROM dbo.QuestResource QRES INNER JOIN dbo.ItemResource IRES ON IRES.id = QRES.value3 INNER JOIN dbo.StringResource SRES ON SRES.code = IRES.name_id WHERE IRES.id = '" + quest_obj_2_id_val + "'";
                        string quest_type_2_string_res = ((string)quest_type_2_string.ExecuteScalar());
                        quest_obj_2_name_val = quest_type_2_string_res.ToString();
                    }

                    //Check for Objective 3
                    if (quest_obj_3_id_val != 0)
                    {
                        bgw.ReportProgress(36, "READING QUEST OBJECTIVE 2");
                        //Load Strings from Items
                        SqlCommand quest_type_3_string = qCon.CreateCommand();
                        quest_type_3_string.CommandText = "SELECT SRES.value FROM dbo.QuestResource QRES INNER JOIN dbo.ItemResource IRES ON IRES.id = QRES.value5 INNER JOIN dbo.StringResource SRES ON SRES.code = IRES.name_id WHERE IRES.id = '" + quest_obj_3_id_val + "'";
                        string quest_type_3_string_res = ((string)quest_type_3_string.ExecuteScalar());
                        quest_obj_3_name_val = quest_type_3_string_res.ToString();
                    }

                    //Set Quest Type Internal Reference
                    quest_type_internal_reference = "ITEM";
                }
                if (quest_type_val == "LEARN_SKILL")
                {
                    //Load Strings from Skills
                    SqlCommand quest_type_string = qCon.CreateCommand();
                    quest_type_string.CommandText = "SELECT SRES.value FROM dbo.QuestResource QRES INNER JOIN dbo.SkillResource SKRES ON SKRES.id = QRES.value1 INNER JOIN dbo.StringResource SRES ON SRES.code = SKRES.text_id WHERE SKRES.id = '" + quest_obj_1_id_val + "'";
                    string quest_type_string_res = ((string)quest_type_string.ExecuteScalar());
                    quest_obj_1_name_val = quest_type_string_res.ToString();

                    //Check for Objective 2 
                    if (quest_obj_2_id_val != 0)
                    {
                        bgw.ReportProgress(34, "READING QUEST OBJECTIVE 2");
                        //Load Strings from Skills
                        SqlCommand quest_type_2_string = qCon.CreateCommand();
                        quest_type_2_string.CommandText = "SELECT SRES.value FROM dbo.QuestResource QRES INNER JOIN dbo.SkillResource SKRES ON SKRES.id = QRES.value3 INNER JOIN dbo.StringResource SRES ON SRES.code = SKRES.text_id WHERE SKRES.id = '" + quest_obj_2_id_val + "'";
                        string quest_type_2_string_res = ((string)quest_type_2_string.ExecuteScalar());
                        quest_obj_2_name_val = quest_type_2_string_res.ToString();
                    }

                    //Check for Objective 3
                    if (quest_obj_3_id_val != 0)
                    {
                        bgw.ReportProgress(36, "READING QUEST OBJECTIVE 2");
                        //Load Strings from Skills
                        SqlCommand quest_type_3_string = qCon.CreateCommand();
                        quest_type_3_string.CommandText = "SELECT SRES.value FROM dbo.QuestResource QRES INNER JOIN dbo.SkillResource SKRES ON SKRES.id = QRES.value5 INNER JOIN dbo.StringResource SRES ON SRES.code = SKRES.text_id WHERE SKRES.id = '" + quest_obj_3_id_val + "'";
                        string quest_type_3_string_res = ((string)quest_type_3_string.ExecuteScalar());
                        quest_obj_3_name_val = quest_type_3_string_res.ToString();
                    }

                    //Set Quest Type Internal Reference
                    quest_type_internal_reference = "SKILL";
                }
            }
            qCon.Close();
        }

        public void apply_list_obj_monster()
        {
            if (quest_obj_1_id_val != 0)
            {
                //Populate obj 1 name box
                quest_obj_1_name_box.DataSource = monster_list.Tables[0];
                quest_obj_1_name_box.DisplayMember = "value";
                quest_obj_1_name_box.ValueMember = "id";
                quest_obj_1_name_box.ResetText();
            }

            if (quest_obj_2_id_val != 0)
            {
                //Populate obj 1 name box
                quest_obj_2_name_box.DataSource = monster_list2.Tables[0];
                quest_obj_2_name_box.DisplayMember = "value";
                quest_obj_2_name_box.ValueMember = "id";
                quest_obj_2_name_box.ResetText();
            }

            if (quest_obj_3_id_val != 0)
            {
                //Populate obj 1 name box
                quest_obj_3_name_box.DataSource = monster_list3.Tables[0];
                quest_obj_3_name_box.DisplayMember = "value";
                quest_obj_3_name_box.ValueMember = "id";
                quest_obj_3_name_box.ResetText();
            }
        }
        public void apply_list_obj_item()
        {
            if (quest_obj_1_id_val != 0)
            {
                //Populate obj 1 name box
                quest_obj_1_name_box.DataSource = item_list.Tables[0];
                quest_obj_1_name_box.DisplayMember = "value";
                quest_obj_1_name_box.ValueMember = "id";
                quest_obj_1_name_box.ResetText();

                if (quest_obj_2_id_val != 0)
                {
                    //Populate obj 1 name box
                    quest_obj_2_name_box.DataSource = item_list1.Tables[0];
                    quest_obj_2_name_box.DisplayMember = "value";
                    quest_obj_2_name_box.ValueMember = "id";
                    quest_obj_2_name_box.ResetText();
                }
            }
        }
        public void apply_list_obj_skill()
        {
            if (quest_obj_1_id_val != 0)
            {
                //Populate obj 1 name box
                quest_obj_1_name_box.DataSource = skill_list.Tables[0];
                quest_obj_1_name_box.DisplayMember = "value";
                quest_obj_1_name_box.ValueMember = "id";
                quest_obj_1_name_box.ResetText();


                if (quest_obj_2_id_val != 0)
                {
                    //Populate obj 1 name box
                    quest_obj_2_name_box.DataSource = skill_list.Tables[0];
                    quest_obj_2_name_box.DisplayMember = "value";
                    quest_obj_2_name_box.ValueMember = "id";
                    quest_obj_2_name_box.ResetText();
                }
            }
        }
    }
}
